package com.draw.cli.gui.panel.home;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.MalformedURLException;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.xml.ws.WebServiceException;

import com.draw.cli.config.ConfigManager;
import com.draw.cli.gui.Window;
import com.draw.cli.gui.panel.Refreshable;
import com.draw.cli.gui.panel.UserTable;
import com.draw.cli.gui.panel.draw.DrawPanel;
import com.draw.cli.webclient.UserClient;
import com.draw.core.User;
import com.draw.webinterface.UserInterface;

public class AvailableDrawPanel extends JPanel implements Refreshable,
		ListSelectionListener, ActionListener, Runnable {
	private static final long serialVersionUID = 6015939217880763417L;

	private static AvailableDrawPanel instance = new AvailableDrawPanel();

	public static AvailableDrawPanel getInstance() {
		return instance;
	}

	// private DefaultListModel<User> model;
	// private JList<User> idents;

	private JButton button;
	private UserTable table;

	public AvailableDrawPanel() {
		this.setBorder(BorderFactory
				.createTitledBorder("Utilisateurs disponibles"));
		this.setLayout(new BorderLayout());
		this.table = new UserTable();
		this.table.getTable().getSelectionModel()
				.addListSelectionListener(this);
		this.table.getTable().getColumnModel().getSelectionModel()
				.addListSelectionListener(this);
		// this.model = new DefaultListModel<User>();
		// this.idents = new JList<User>(this.model);
		// this.idents.addListSelectionListener(this);
		// JScrollPane scroll = new JScrollPane(this.idents);
		// this.add(scroll);
		this.add(this.table);
		button = new JButton("Dessiner");
		button.setEnabled(false);
		button.addActionListener(this);
		this.add(button, BorderLayout.SOUTH);
	}

	@Override
	public void refresh() {
		new Thread(this).start();
	}

	@Override
	public void valueChanged(ListSelectionEvent arg0) {
		User current = ConfigManager.getInstance().getCurrentUser();
		//User selected = this.idents.getSelectedValue();
		User selected = this.table.getSelectedUser();
		if (current != null && selected != null
				&& current.getId() != selected.getId()) {
			this.button.setEnabled(true);
		} else {
			this.button.setEnabled(false);
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		this.button.setEnabled(false);
		User sel = this.table.getSelectedUser();
		if (e.getSource().equals(this.button) && sel != null) {
			try {
				DrawPanel.getInstance().initialize(
						ConfigManager.getInstance().getCurrentUser(), sel);
				Window.getInstance().setDraw();
			} catch (MalformedURLException | WebServiceException e1) {
				e1.printStackTrace();
				JOptionPane.showMessageDialog(Window.getInstance(),
						"Erreur à l'initialisation.", "Initialisation.",
						JOptionPane.ERROR_MESSAGE);
				Window.getInstance().setHome();
			}
		}
	}

	@Override
	public void run() {
		this.button.setEnabled(false);
		ArrayList<User> users = new ArrayList<User>();
		this.table.setUsers(users);
		users = new ArrayList<User>();
		User current = ConfigManager.getInstance().getCurrentUser();
		try {
			UserInterface ui = UserClient.getInstance();
			if ( current != null ) {
				for (User user : ui.getAvailableUserToDraw(current) ) {
					if (!user.getUsername().equals(current.getUsername())) {
						users.add(user);
					}
				}
			} else {
				for (User user : ui.getAll()) {
					users.add(user);
				}
			}
//			for (User user : ui.getAll()) {
//				if (current == null || current.getId() != user.getId()) {
//					DrawingInterface di = DrawingClient.getInstance();
//					List<Drawing> ld = di.getDrawingSent(current);
//					boolean can = true;
//					for (int i = 0; i < ld.size() && can; i++) {
//						can = !user.equals(ld.get(i).getDestination());
//					}
//					if (can) {
//						ld = di.getDrawingReceived(current);
//						for (int i = 0; i < ld.size() && can; i++) {
//							can = !user.equals(ld.get(i).getSource());
//						}
//					}
//					if (can) {
//						// this.model.addElement(user);
//						users.add(user);
//					}
//				}
		} catch (MalformedURLException | WebServiceException e) {
			// e.printStackTrace();
		}
		this.table.setUsers(users);
	}
}
